package com.mh.framework.service;

import com.mh.system.domain.SysUser;
import com.mh.system.service.SysMenuService;
import com.mh.system.service.SysRoleService;
import com.mh.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

//统一获取用户角色&权限service
@Component
public class SysPermsService {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysMenuService menuService;

    //通用获取角色,不用每次都注入service,再获取
    public Set<String> getRoles(SysUser user) {

        Set<String> roles = new HashSet();

        //数据库中,admin的角色是null,因为admin什么角色都有,所以没有一一列出
        //因此,在此调用isAdmin()方法判断是否为超级管理员(id为1)
        if(user.isAdmin()){
            //是admin的话,在集合中添加"admin"的角色(因为数据库是空的)
            roles.add("admin");
        }else {
            //不是admin的话,正常调用service查询角色集合
            roles = roleService.selectRoleKeysByUserId(user.getUserId());
        }
        return roles;
    }
    //通用获取权限
    public Set<String> getPerms(SysUser user) {

        Set<String> perms = new HashSet();

        if(user.isAdmin()){
            //是admin的话,在集合中添加"admin"的权限,什么都有(因为数据库是空的)
            perms.add("*:*:*");
        }else {
            //不是admin的话,正常调用service查询角色集合
            perms = menuService.selectPermsByUserId(user.getUserId());
        }
        return perms;
    }
}
